//
//  VisitorLoginView.swift
//  SinaWeibo
//
//  Created by bot on 16/5/13.
//  Copyright © 2016年 bot. All rights reserved.
//

import UIKit

class VisitorLoginView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    func setUIInfo(tipText: String?, iconImage: String?) {
        if let imgName = iconImage {
            iconImageView.image = UIImage(named: imgName)
            circleImageView.hidden = true
            
            if let text = tipText {
                tipLabel.text = text
            }
        }
        else {
            circleImageView.hidden = false
        }
    }
    
   

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(circleImageView)
        addSubview(backCoverView)
        addSubview(iconImageView)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //设置背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        startAnimation()
    }
    
    //circleImageView动画设置
    private func startAnimation() {
        
        let animation = CABasicAnimation(keyPath: "transform.rotation.z")
        animation.toValue = M_PI * 2
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        animation.duration = 20
        
        circleImageView.layer.addAnimation(animation, forKey: nil)
        
    }
    
     override func layoutSubviews() {
        super.layoutSubviews()
        
        for subview in subviews {
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        
       //设置circleImageView的约束
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -50))
        
        //设置iconImageView的约束
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterX, relatedBy: .Equal, toItem: circleImageView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImageView, attribute: .CenterY, relatedBy: .Equal, toItem: circleImageView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //设置tipLabel的约束
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .CenterX, relatedBy: .Equal, toItem: circleImageView, attribute: .CenterX, multiplier: 1.0, constant: 1))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Top, relatedBy: .Equal, toItem: circleImageView, attribute: .Bottom, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 228))
        addConstraint(NSLayoutConstraint(item: tipLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 50))
        
        //添加登录按钮约束
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: tipLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //添加注册按钮约束
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: tipLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: tipLabel, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //添加遮罩view的约束
        addConstraint(NSLayoutConstraint(item: backCoverView, attribute: .Left, relatedBy: .Equal, toItem: circleImageView, attribute: .Left, multiplier: 1.0, constant: 1))
        addConstraint(NSLayoutConstraint(item: backCoverView, attribute: .Right, relatedBy: .Equal, toItem: circleImageView, attribute: .Right, multiplier: 1.0, constant: 1))
        addConstraint(NSLayoutConstraint(item: backCoverView, attribute: .Bottom, relatedBy: .Equal, toItem: circleImageView, attribute: .Bottom, multiplier: 1.0, constant: -10))
        
        
    }
    
    //懒加载所有控件
    lazy var iconImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    lazy var circleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    lazy var tipLabel:UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.numberOfLines = 0
        return label
    }()
    lazy var loginBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
        return btn
    }()
    lazy var registerBtn:UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        return btn
    }()
    
    lazy var backCoverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    

}
